package com.hluck.goodweatherapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hluck.goodweatherapp.data.bean.Province;
import com.hluck.goodweatherapp.databinding.ItemTextRvBinding;
import com.hluck.goodweatherapp.utils.AdministrativeType;

import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/4
 * @description：
 */
public class ProvinceAdapter extends RecyclerView.Adapter<ProvinceAdapter.ViewHolder> {

    private final List<Province> provinces;

    private AdministrativeClickCallback administrativeClickCallback; //视图点击

    public ProvinceAdapter(List<Province> provinceList){
        this.provinces = provinceList;
    }

    public void setAdministrativeClickCallback(AdministrativeClickCallback administrativeClickCallback) {
        this.administrativeClickCallback = administrativeClickCallback;
    }

    @NonNull
    @org.jetbrains.annotations.NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
        ItemTextRvBinding binding = ItemTextRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder holder = new ViewHolder(binding);

        binding.getRoot().setOnClickListener(v -> {
            if (administrativeClickCallback != null){
                administrativeClickCallback.onAdministrativeItemClick(v,holder.getAdapterPosition(), AdministrativeType.PROVINCE);
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull ProvinceAdapter.ViewHolder holder, int position) {
        holder.mbinding.tvText.setText(provinces.get(position).getProvinceName());
    }

    @Override
    public int getItemCount() {
        return provinces.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ItemTextRvBinding mbinding;

        public ViewHolder(ItemTextRvBinding binding) {
            super(binding.getRoot());
            mbinding = binding;
        }
    }
}
